package com.he.controller;

import com.he.annotation.OperationLogger;
import com.he.domain.Result;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.LinkDTO;
import com.he.domain.dto.FriendLinkDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.vo.LinkBackVO;
import com.he.service.LinkService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Positive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/15 21:27
 * @Desc
 */
@Tag(name = "链接")
@RestController
public class LinkController {

    @Resource
    private LinkService linkService;

    @Operation(description = "获取友链列表")
    @GetMapping("/link/friend")
    public Result<?> getFriendLinkVOList(){
        return Result.success(linkService.getFriendLinkVOList());
    }

    @Operation(description = "添加友链")
    @PostMapping("/link/add")
    public Result<?> addFriendLink(@Validated @RequestBody FriendLinkDTO friendLinkDTO){
        linkService.addFriendLink(friendLinkDTO);
        return Result.success();
    }

    @Operation(description = "删除友链")
    @DeleteMapping("/link/delete")
    public Result<?> deleteFriendLink(@Positive @RequestParam Integer id){
        linkService.deleteFriendLink(id);
        return Result.success();
    }

    @Operation(description = "更新友链")
    @PostMapping("/link/update")
    public Result<?> updateFriendLink(@RequestBody FriendLinkDTO friendLinkDTO){
        linkService.updateFriendLink(friendLinkDTO);
        return Result.success();
    }

    @Operation(description = "获取后台链接列表")
    @PreAuthorize("@ps.hasPermission('web:link:list')")
    @GetMapping("/admin/link/list")
    public Result<PageResult<LinkBackVO>> getLinkBackVOList(ConditionDTO condition){
        return Result.success(linkService.getLinkBackVOList(condition));
    }

    @OperationLogger(value = "添加链接")
    @Operation(description = "添加链接")
    @PreAuthorize("@ps.hasPermission('web:link:add')")
    @PostMapping("/admin/link/add")
    public Result<?> addLink(@Validated @RequestBody LinkDTO linkDTO) {
        linkService.addLink(linkDTO);
        return Result.success();
    }

    @OperationLogger(value = "删除链接")
    @Operation(description = "删除链接")
    @PreAuthorize("@ps.hasPermission('web:link:delete')")
    @DeleteMapping("/admin/link/delete")
    public Result<?> deleteFriend(@RequestBody List<Integer> linkIdList) {
        linkService.removeByIds(linkIdList);
        return Result.success();
    }

    @OperationLogger(value = "修改链接")
    @Operation(description = "修改链接")
    @PreAuthorize("@ps.hasPermission('web:link:update')")
    @PutMapping("/admin/link/update")
    public Result<?> updateFriend(@Validated @RequestBody LinkDTO linkDTO) {
        linkService.updateLink(linkDTO);
        return Result.success();
    }



}
